﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
namespace PhotonControls
{
    public class FZoomToRectTool : FImageTool
    {
        private FImage m_image;
        private int m_Left;
        private int m_Right;
        private int m_Top;
        private int m_Bottom;
        private bool m_pressed;
        private Bitmap m_bg;
        private void DrawSelRect()
        {
            Bitmap _bmp = new Bitmap(m_bg);
            Graphics g = Graphics.FromImage(_bmp);
            g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;
            g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed;

            Pen _pen = new Pen(Color.Red);
            _pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
            Rectangle _r = new Rectangle(m_Left, m_Top, m_Right - m_Left, m_Bottom - m_Top);
            g.DrawRectangle(_pen, _r);
            m_image.Background = _bmp;
        }

        public FZoomToRectTool(FImage _image)
        {
            m_image = _image;
            m_pressed = false;
            m_Left = 0;
            m_Right = 0;
            m_Top = 0;
            m_Bottom = 0;
        }
        public void MouseDown(MouseEventArgs e)
        {
            if (m_image == null)
                return;
            if (e.Button == MouseButtons.Left)
            {
                m_Left = e.X;
                m_Right = e.X;
                m_Top = e.Y;
                m_Bottom = e.Y;
                m_pressed = true;
                m_bg = m_image.Background;
            }
        }
        public void MouseUp(MouseEventArgs e)
        {
            if (m_image == null)
                return;
            if (e.Button == MouseButtons.Left)
            {
                if (e.X < m_Left)
                    m_Left = e.X;
                else 
                    m_Right = e.X;
                if (e.Y < m_Top)
                    m_Top = e.Y;
                else 
                    m_Bottom = e.Y;
                
                Rectangle r = new Rectangle(m_Left, m_Top, m_Right - m_Left, m_Bottom - m_Top);
                m_image.ZoomToRect(r);
                m_pressed = false;
            }
        }
        public void MouseMove(MouseEventArgs e)
        {
            if (m_image == null)
                return;
            if (m_pressed && e.Button == MouseButtons.Left)
            {
                if (e.X < m_Left)
                    m_Left = e.X;
                else
                    m_Right = e.X;
                if (e.Y < m_Top)
                    m_Top = e.Y;
                else
                    m_Bottom = e.Y;
                DrawSelRect();
            }
        }
        public FImage Image
        {
            get { return m_image; }
            set { m_image = value; }
        }
    }
}
